package pl.com.kyhu.dietex3000.GUI.cards;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import pl.com.kyhu.dietex3000.GUI.components.NutrientsFace;
import pl.com.kyhu.dietex3000.database.beans.Nutrients;

public class NutritionCard extends Composite {

	private static final String LIMIT = "Ograniczenie";

	private Text daysText;
	private Text mealsText;
	NutrientsFace goalNutrients;
	private Text sameMealLimit;
	private Text maxAmountLimit;
	private Text minAmountLimit;

	Button sameMealButton, maxAmountButton, minAmountButton;

	public NutritionCard(Composite parent, int style) {
		super(parent, style);
		setLayout(null);

		goalNutrients = new NutrientsFace(this, SWT.NONE);
		goalNutrients.setText("Warto\u015Bci od\u017Cywcze");
		goalNutrients.setBounds(151, 10, 353, 308);

		Group grpPlan = new Group(this, SWT.NONE);
		grpPlan.setText("Plan");
		grpPlan.setBounds(10, 10, 135, 78);

		Label daysLabel = new Label(grpPlan, SWT.CENTER);
		daysLabel.setBounds(10, 16, 85, 22);
		daysLabel.setAlignment(SWT.RIGHT);
		daysLabel.setText("Liczba dni");

		Label mealsLabel = new Label(grpPlan, SWT.NONE);
		mealsLabel.setBounds(10, 45, 85, 19);
		mealsLabel.setAlignment(SWT.RIGHT);
		mealsLabel.setText("Liczba posi\u0142k\u00F3w");

		mealsText = new Text(grpPlan, SWT.BORDER | SWT.CENTER);
		mealsText.setBounds(101, 42, 22, 19);
		mealsText.setText("6");

		daysText = new Text(grpPlan, SWT.BORDER | SWT.CENTER);
		daysText.setBounds(101, 13, 22, 19);
		daysText.setText("30");

		Group grpLimits = new Group(this, SWT.NONE);
		grpLimits.setText("Ograniczenia");
		grpLimits.setBounds(10, 91, 135, 227);

		sameMealButton = new Button(grpLimits, SWT.CHECK);
		sameMealButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				sameMealLimit.setEnabled(sameMealButton.getSelection());
				sameMealLimit.setText(LIMIT);
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				widgetDefaultSelected(e);
			}
		});
		sameMealButton.setBounds(10, 25, 13, 22);

		sameMealLimit = new Text(grpLimits, SWT.BORDER);
		sameMealLimit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				if (sameMealLimit.isEnabled() && sameMealLimit.getText().equals(LIMIT)) {
					sameMealLimit.setText("");
				}
			}
		});
		sameMealLimit.setText(LIMIT);
		sameMealLimit.setBounds(15, 50, 95, 22);
		sameMealLimit.setEnabled(false);

		maxAmountButton = new Button(grpLimits, SWT.CHECK);
		maxAmountButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				maxAmountLimit.setEnabled(maxAmountButton.getSelection());
				maxAmountLimit.setText(LIMIT);
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				widgetDefaultSelected(e);
			}
		});
		maxAmountButton.setText("Maks waga posi\u0142ku");
		maxAmountButton.setBounds(10, 85, 115, 24);

		maxAmountLimit = new Text(grpLimits, SWT.BORDER);
		maxAmountLimit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				if (maxAmountLimit.isEnabled() && maxAmountLimit.getText().equals(LIMIT)) {
					maxAmountLimit.setText("");
				}
			}
		});
		maxAmountLimit.setText(LIMIT);
		maxAmountLimit.setBounds(15, 110, 95, 22);
		maxAmountLimit.setEnabled(false);

		minAmountButton = new Button(grpLimits, SWT.CHECK);
		minAmountButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				minAmountLimit.setEnabled(minAmountButton.getSelection());
				minAmountLimit.setText(LIMIT);
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				widgetDefaultSelected(e);
			}
		});
		minAmountButton.setText("Min waga posi\u0142ku");
		minAmountButton.setBounds(10, 145, 115, 24);

		minAmountLimit = new Text(grpLimits, SWT.BORDER);
		minAmountLimit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				if (minAmountLimit.isEnabled() && minAmountLimit.getText().equals(LIMIT)) {
					minAmountLimit.setText("");
				}
			}
		});
		minAmountLimit.setText(LIMIT);
		minAmountLimit.setBounds(15, 170, 95, 22);
		minAmountLimit.setEnabled(false);

		Label lblMaksLiczbaJednej = new Label(grpLimits, SWT.WRAP);
		lblMaksLiczbaJednej.setFont(SWTResourceManager.getFont("Microsoft Sans Serif", 7, SWT.NORMAL));
		lblMaksLiczbaJednej.setBounds(29, 25, 100, 24);
		lblMaksLiczbaJednej.setText("Maks. liczba jednej potrawy w diecie");
		goalNutrients.setDefaultNutrients();

	}

	public Map<Integer, Float> getLimits() {
		Map<Integer, Float> limits = new HashMap<Integer, Float>();
		try {
			if (sameMealButton.getSelection()) limits.put(1, Float.parseFloat(sameMealLimit.getText()));
			if (maxAmountButton.getSelection()) limits.put(2, Float.parseFloat(maxAmountLimit.getText()));
			if (minAmountButton.getSelection()) limits.put(3, Float.parseFloat(minAmountLimit.getText()));
		} catch (NumberFormatException nfe) {
			nfe.printStackTrace();
		}
		return limits;
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public Integer getDaysNumber() {
		return Integer.parseInt(daysText.getText());
	}

	public Integer getMealsNumber() {
		return Integer.parseInt(mealsText.getText());
	}

	public Nutrients getNutrients() {
		return goalNutrients.getNutrients();
	}

}
